package jp.ne.ayamesoft.ayamevoicedialer;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SQLHelper_MemberList extends SQLiteOpenHelper {
	final int INT_MEMBER_MAX = 20;
	Resources resources;
	String STR_DB_NAME;
	String STR_TABLE_NAME;
	String STR_NODATA;
	SQLiteDatabase member_db;

	public SQLHelper_MemberList(Context context, String name,
			CursorFactory factory) {
		super(context, name, factory, 1);
		// TODO Auto-generated constructor stub
		resources = context.getResources();
		STR_TABLE_NAME = resources.getString(R.string.tablename_memberlist);
		STR_NODATA = resources.getString(R.string.no_data);
		STR_DB_NAME = name;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String STR_INITIAL_TABLE = "create table "
				+ STR_TABLE_NAME
				+ " ( id integer primary key, nickname text not null, name text not null, number text not null, atrb integer not null );";
		String STR_INSERT_SQL = "";
		int pid = 0;
		// TODO Auto-generated method stub
		db.beginTransaction();
		try {
			db.execSQL(STR_INITIAL_TABLE);
			for (int i = 0; i < INT_MEMBER_MAX; i++) {
				STR_INSERT_SQL = "insert into " + STR_TABLE_NAME
						+ "(id,nickname,name,number,atrb) values " + "(" + pid
						+ "," + "'" + STR_NODATA + "'" + "," + "'" + STR_NODATA
						+ "'" + "," + "'" + STR_NODATA + "'" + "," + "0" + ");";
				db.execSQL(STR_INSERT_SQL);
				// db.execSQL("insert into mytable(id,name,atrb) values (3, '3番目',14);");
				pid++;
			}
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		// TODO Auto-generated method stub

	}

	public void Update_atrb_By_id(SQLiteDatabase db, int id, int in_atrb) {
		Update_all_By_id(db, id, null, null, null, in_atrb);
	}

	public void Update_nickname_By_id(SQLiteDatabase db, int id,
			String in_nickname) {
		Update_all_By_id(db, id, in_nickname, null, null, -1);
	} 

	public void Update_name_By_id(SQLiteDatabase db, int id, String in_name) {
		Update_all_By_id(db, id, null, in_name, null, -1);
	} 

	public void Update_number_By_id(SQLiteDatabase db, int id, String in_number) {
		Update_all_By_id(db, id, null, null, in_number, -1);
	}

	public void Update_all_By_id(SQLiteDatabase db, int id, String in_nickname,
			String in_name, String in_number, int in_atrb) {
		
		try {

			String where = "id = " + String.valueOf(id);
			ContentValues cv = new ContentValues();
			if (in_nickname != null) {
				cv.put("nickname", in_nickname);
				AyameLOG.D("Update nickname:[" + in_nickname + "]");
			}
			if (in_name != null) {
				cv.put("name", in_name);
				AyameLOG.D("Update name:[" + in_name + "]");
			}
			if (in_number != null) {
				cv.put("number", in_number);
				AyameLOG.D("Update number:[" + in_number + "]");
			}
			if (in_atrb != -1) {
				cv.put("atrb", in_atrb);
				AyameLOG.D("Update atrb:[" + String.valueOf(in_atrb) + "]");
			}
			db.update(STR_TABLE_NAME, cv, where, null);
			AyameLOG.D("Update Success:id[" + String.valueOf(id) + "]");
		} catch (SQLException e) {
			AyameLOG.D("Update Error:id[" + String.valueOf(id) + "]");
		}
	}

	public void Clear_atrb_By_id(SQLiteDatabase db, int id) {

		try {
			String where = "id = " + String.valueOf(id);
			ContentValues cv = new ContentValues();
			cv.put("atrb", 0);
			db.update(STR_TABLE_NAME, cv, where, null);
			AyameLOG.D("Clear atrb Success: id[" + String.valueOf(id) + "]");
		} catch (SQLException e) {
			AyameLOG.D("Clear atrb Error: id[" + String.valueOf(id) + "]");
		}
		
	}

	public void Clear_all_By_id(SQLiteDatabase db, int id) {

		try {
			String where = "id = " + String.valueOf(id);
			ContentValues cv = new ContentValues();
			cv.put("atrb", 0);
			cv.put("nickname", STR_NODATA);
			cv.put("name", STR_NODATA);
			cv.put("number", STR_NODATA);
			db.update(STR_TABLE_NAME, cv, where, null);
			AyameLOG.D("Clear all Success: id[" + String.valueOf(id) + "]");
		} catch (SQLException e) {
			AyameLOG.D("Clear all Error: id[" + String.valueOf(id) + "]");
		}
	}
}
